<%-- 
    Document   : Sp_CenterContainer_Cart
    Created on : Apr 1, 2015, 9:52:20 AM
    Author     : PHATNBSE61057
--%>

<%@page import="personal.dao.BookDAO"%>
<%@page import="personal.obj.Book"%>
<%@page import="java.util.Map.Entry"%>
<%@page import="java.util.Map"%>
<%@page import="java.util.Iterator"%>
<%@page import="java.util.HashMap"%>
<%@page import="personal.obj.Cart"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<c:set var="cartError" value="${requestScope.CARTERROR}"/>
<c:set var="cartEmpty" value="${sessionScope.CART}"/>

<td class="centerContainer">
    <div class="c1">
        <div class="c2">
            <div class="top"></div>
        </div>
    </div>
    <div class="content">
        <div class="content-wrapper">
            <a name="main"></a>
            <span id="serch_error">
            </span>
            <div id="pageTitle" class="pageTitleSection">
                <div class="block">
                    <div class="cont">
                        <h2><span>In your cart</span></h2>
                    </div>
                </div>
            </div>

            <c:if test="${empty cartEmpty}">
                <div class="shoppingCartSection">
                    <div class="block">
                        <div class="corner1">
                            <div class="corner2">
                                <div class="lineTop"></div>
                            </div>
                        </div>
                        <div class="cont">
                            <p>Your Cart is empty</p>
                        </div>
                        <div class="corner4">
                            <div class="corner3">
                                <div class="lineBtm"></div>
                            </div>
                        </div>
                    </div>
                </div>
            </c:if>

            <c:if test="${not empty cartEmpty}">
                <div class="shoppingCartSection">
                    <div class="block">
                        <div class="cont">
                            <form action="ProcessServlet" method="POST">
                                <span id="shoppingcart_error">
                                    <c:if test="${not empty cartError}">
                                        <p class="error">Invalid quantity. Please check and try again.</p>
                                    </c:if>
                                </span>

                                <div class="checkoutBtnWrapper checkoutBtnWrapperTop">
                                    <a class="smallBtn leftBtn continueShopping" href="ProcessServlet?btAction=GetCategory&categoryID=C0002">Continue Shopping</a>
                                    <input class="smallBtn rightBtn" value="Checkout" type="submit" name="btAction">
                                    <input type="hidden" value="1" name="CheckOutProcess"/>
                                </div>

                                <div class="wrapperShoppingCartDet">

                                    <div class="shoppingCartDet">
                                        <table class="base">
                                            <thead>
                                                <tr>
                                                    <th colspan="2" class="productName">Book Name</th>
                                                    <th>Quantity</th>
                                                    <th class="price">Price</th>
                                                    <th class="subtotal">Subtotal</th>
                                                    <th class="del">Remove</th>
                                                </tr>
                                            </thead>
                                            <tbody>

                                                <%
                                                    if (session != null) {
                                                        Cart cart = (Cart) session.getAttribute("CART");
                                                        if (cart != null) {
                                                            HashMap items = cart.getItems();
                                                            if (!items.isEmpty()) {
                                                                Iterator iter = items.entrySet().iterator();
                                                                float sum = 0;
                                                                int sumQuantity = 0;
                                                                while (iter.hasNext()) {
                                                                    Map.Entry item = (Entry) iter.next();
                                                                    Book book = new Book();
                                                                    BookDAO bDAO = new BookDAO();
                                                                    book = bDAO.getBookByBookID(item.getKey().toString());
                                                                    if (book != null) {
                                                %>
                                                <tr class="">
                                                    <td class="productImage grid_2">
                                                        <a href="ProcessServlet?btAction=ProductDetail&productID=<%=book.getBookID()%>">
                                                            <img alt="<%=book.getBookName()%>" title="<%=book.getBookName()%>" src="<%=book.getBookThumb()%>">
                                                        </a>
                                                    </td>
                                                    <td class="productName">
                                                        <a href="ProcessServlet?btAction=ProductDetail&productID=<%=book.getBookID()%>" style="max-width: 144px">
                                                            <%=book.getBookName()%>
                                                        </a>
                                                    </td>
                                                    <td>
                                                        <input type="hidden" name="txtBookID" value="<%=item.getKey()%>" />
                                                        <input class="textInput" name="txtQuantity" value="<%=item.getValue()%>" type="text" maxlength="7">
                                                    </td>
                                                    <td class="price"><%=book.getBookPrice()%></td>

                                                    <%
                                                        String sUnitPrice = book.getBookPrice();
                                                        //Parse sang float
                                                        float fUnitPrice = Float.parseFloat(sUnitPrice.substring(1, sUnitPrice.length()));
                                                        float total = fUnitPrice * (Integer) item.getValue();
                                                        sum += total;
                                                        sumQuantity += (Integer) item.getValue();
                                                        session.setAttribute("SUBTOTAL", sum);
                                                        session.setAttribute("SUMQUANTITY", sumQuantity);
                                                    %>

                                                    <td class="subtotal">£<%=total%></td>
                                                    <td class="del">
                                                        <form action="ProcessServlet" method="POST">
                                                            <button class="dlt" type="submit" name="btAction" value="RemoveItems"></button>
                                                            <input type="hidden" name="removeBookID" value="<%=book.getBookID()%>"/>
                                                        </form>
                                                    </td>
                                                </tr>

                                                <%
                                                                    }
                                                                }
                                                            }
                                                        }
                                                    }
                                                %>
                                            </tbody>
                                        </table>
                                    </div>
                                    <input class="smallBtn leftBtn updateBtn" value="Update" type="submit" name="btAction">

                                    <div class="nofloat"></div>
                                </div>

                                <div class="wrapperTotals">
                                    <div id="totals" class="totals"><h4>Subtotal</h4>
                                        <table id="total" class="totalBlock">
                                            <tbody>
                                                <tr class="productSubtotal">
                                                    <td><span>Product Subtotal</span></td>
                                                    <td class="price last"><span>£${sessionScope.SUBTOTAL}</span></td>
                                                </tr>
                                                <tr class="shipping">
                                                    <td><span>Shipping</span></td>
                                                    <td class="price last"><span>£0.00</span></td>
                                                </tr>
                                                <tr class="total">
                                                    <td><span>Total</span></td>
                                                    <td class="price last"><span>£${sessionScope.SUBTOTAL}</span></td>
                                                </tr>
                                            </tbody>
                                        </table>
                                    </div>
                                </div>

                                <div class="nofloat"></div>

                                <div class="checkoutBtnWrapper checkoutBtnWrapperBtm">
                                    <a class="smallBtn leftBtn continueShopping" href="ProcessServlet?btAction=GetCategory&categoryID=C0002">Continue Shopping</a>
                                    <input class="smallBtn rightBtn" value="Checkout" type="submit" name="btAction">
                                </div>
                            </form>
                        </div>
                        <div class="corner4">
                            <div class="corner3">
                                <div class="lineBtm"></div>
                            </div>
                        </div>
                    </div>
                    <div class="nofloat"></div>
                </div>
            </c:if>
        </div>
    </div>
    <div class="c4">
        <div class="c3">
            <div class="btm"></div>
        </div>
    </div>
</td>